/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.company;

import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import jobportal.Dbconn;

/**
 *
 * @author project
 */
public class CompanyLogin extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            HttpSession session = request.getSession();
            Connection con = Dbconn.getConn();
            Statement st = con.createStatement();
            ResultSet rs;
            String username, password, sql;
            username = request.getParameter("ename");
            password = request.getParameter("epass");
            sql = "select compid,password,compname from company where compid='" + username + "'";

            out.println(username + password);

            rs = st.executeQuery(sql);
            if (rs.next()) {
                if (rs.getString("password").equals(password)) {

                    request.getSession();
                    session.setAttribute("role", "company");
                    session.setAttribute("username", rs.getString("compid"));
                    session.setAttribute("name", rs.getString("compname"));
                    st.executeUpdate("Update company set lastlogin=now() where compid='" + rs.getString("compid") + "'");
                    response.sendRedirect("Company/chome.jsp");
                } else {
                    session.invalidate();
                    out.println("<red>Invalid Password</red>");
                    response.sendRedirect("index.jsp");
                }
            } else {
                session.invalidate();
                out.println("<red>Username does not exist</red>");
                response.sendRedirect("index.jsp");
            }

        } catch (Exception e) {
            out.println("Exception:: " + e);
        }
    }
}
